﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VarietyList.ascx.cs" Inherits="com.cngrain.SAFC.Web.res.inc.VarietyList" %>
<link href="../res/css/platform/tanchu.css" rel="stylesheet" type="text/css" />

<script src="../res/js/common/Dialog.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

    function HandleCheckbox(e) {
        //e = window.event || e;
        //alert(e.target.type);
        var element = document.all ? e.srcElement : e.target;
        //alert(element.tagName);
        if (element.tagName == "INPUT" && element.type == "checkbox") {
            //alert('');
            var checkedState = element.checked;
            while (element.tagName != "TABLE") // Get wrapping table               
            {
                element = element.parentNode;
            }
            var parentElement = element;

            if (checkedState) {
                CheckParents(element);
            }
            element = element.nextSibling; //element.tagName = DIV               

            if (element != null) // If no childrens then exit
            {
                if (element.tagName) {
                    var childTables = element.getElementsByTagName("TABLE");

                    for (var tableIndex = 0; tableIndex < childTables.length; tableIndex++) {
                        CheckTable(childTables[tableIndex], checkedState);
                    }
                }
            }
            if (checkedState == false) {
                UnCheckParents(parentElement);
            }
        }
    }

    // Uncheck the parents of the given table, Can remove the recurse (redundant) 
    function CheckParents(table) {
        if (table == null || table.rows[0].cells.length == 2) // This is the root 
        {
            return;
        }
        var parentTable = table.parentNode.previousSibling;
        CheckTable(parentTable, true);
        CheckParents(parentTable);
    }

    // Check the parents of the given table, Can remove the recurse (redundant) 
    function UnCheckParents(table) {

        if (table == null || table.rows[0].cells.length == 2) // This is the root  
        {
            return;
        }
        var parentTable = table.parentNode.previousSibling;

        var checkedCount = GetCheckedCount(table.parentNode);
        if (checkedCount == 0) {
            CheckTable(parentTable, false);
        }
        UnCheckParents(parentTable);
    }

    // Handle the set of checkbox checked state                                    
    function CheckTable(table, checked) {
        var checkboxIndex = table.rows[0].cells.length - 1;
        var cell = table.rows[0].cells[checkboxIndex];
        var checkboxes = cell.getElementsByTagName("INPUT");
        if (checkboxes.length == 1) {
            checkboxes[0].checked = checked;
        }
    }
    //Get checked children count
    function GetCheckedCount(table) {
        var checkedCount = 0;
        var element = table.nextSibling;
        var childTable = table.getElementsByTagName("TABLE");

        for (var tableIndex = 0; tableIndex < childTable.length; tableIndex++) {
            var childTables = childTable[tableIndex];
            var checkboxIndex = childTables.rows[0].cells.length - 1;
            var cell = childTables.rows[0].cells[checkboxIndex];
            var checkboxes = cell.getElementsByTagName("INPUT");
            if (checkboxes.length == 1 && checkboxes[0].checked == true) {
                checkedCount++;
            }
        }
        return checkedCount;
    }

    function updateDefaultPage() {
        var n = window.top.center.getComponent('CustomMenu');
        window.top.center.remove(n);
    }

    function setKinds() {
        var kindsStr = '';
        var kindsString = '';
        var treeView = document.getElementById('<%=trvMenu.ClientID %>');
        var objects = treeView.getElementsByTagName("input");
        var e = null;
        for (var i = 0; i < objects.length; i++) {
            e = objects[i];
            if (e.type == "checkbox" && (e.id.indexOf("trvMenu") != -1)) {
                if (e.checked) {
                    //alert(e.nextSibling.firstChild.nodeValue)得到的是名称
                    if (e.title != null && e.title != "") {
                        kindsStr += e.title + ',';
                        kindsString += e.nextSibling.firstChild.nodeValue + ',';
                    }
                }
            }
        }
        if (kindsStr.length != 0)
            kindsStr = kindsStr.substring(0, kindsStr.length - 1);
        if (kindsString.length != 0)
            kindsString = kindsString.substring(0, kindsString.length - 1);

        document.getElementById('<%=txtVarietyCodes.ClientID %>').value = kindsString;
        document.getElementById("<%=hfdVCodes.ClientID %>").value = kindsStr;
        document.getElementById("<%=hfdVNames.ClientID %>").value = kindsString;
        TopDivHideByVeriety();
    }

    function selectAll() {
        var chbObj = document.getElementById('checkAll');
        var treeView = document.getElementById('<%=trvMenu.ClientID %>');
        var objects = treeView.getElementsByTagName("input");
        var e = null;
        for (var i = 0; i < objects.length; i++) {
            e = objects[i];
            if (e.type == "checkbox" && (e.id.indexOf("trvMenu") != -1)) {
                e.checked = chbObj.checked;
            }
        }
    }    
</script>

<asp:HiddenField ID="hfdVCodes" runat="server" />
<asp:HiddenField ID="hfdVNames" runat="server" />
<asp:HiddenField ID="hfdGCodes" runat="server" />
<asp:HiddenField ID="hfdGNames" runat="server" />
<div style="white-space: nowrap; vertical-align: middle;">
    <div class="dropinput" onclick="showTopDivByVeriety('DivAcByVeriety','<%=txtVarietyCodes.ClientID %>',2,20);">
        <asp:TextBox ID="txtVarietyCodes" runat="server" BorderWidth="0"></asp:TextBox>
    </div>
    <span id="show"></span>
</div>
<div id="DivAcByVeriety" class="DIVAC" style="position: absolute; display: none;
    width: 250px" onmouseover="holdingTopDivByVeriety();" onmouseout="hideTopDivByVeriety();">
    <div class="titlebg1" style="width: 100%;">
        <div class="fl1 row1">
            <img src="../res/images/home/row.gif" width="7" height="7" border="0" /></div>
        <div class="fl1 title1">
            粮食品种列表</div>
        <div class="clear1">
        </div>
    </div>
    <div class="titbg1">
        <div class="button8">
            全选<input id="checkAll" type="checkbox" value="12" onclick="selectAll()" />
            <a href="javascript:void(0);" onclick="setKinds()">确 定</a> <a href="javascript:void(0);"
                onclick="TopDivHideByVeriety();">关 闭</a>
        </div>
        <div class="popli">
            <asp:TreeView ID="trvMenu" runat="server" BackColor="#F2F6FA" onclick="HandleCheckbox(event);"
                ImageSet="Simple" NodeIndent="10" ShowLines="True" ExpandDepth="0" ShowCheckBoxes="All"
                PopulateNodesFromClient="False">
                <HoverNodeStyle Font-Underline="True" ForeColor="#DD5555" />
                <SelectedNodeStyle Font-Underline="True" ForeColor="#DD5555" HorizontalPadding="0px"
                    VerticalPadding="0px" />
            </asp:TreeView>
        </div>
    </div>
</div>

<script type="text/javascript">
    var TopDivByVeriety;
    var TopTimeByVeriety;
    function showTopDivByVeriety(DivId, ItemId, offsetX, offset, args) {
        if (TopDivByVeriety != null)
            if (TopDivByVeriety.id != DivId) {
            TopDivByVeriety.Hide();
        }
        window.clearTimeout(TopTimeByVeriety);
        TopDivByVeriety = new Dialog(DivId, null, null);
        TopDivByVeriety.Show();
        TopDivByVeriety.SetWithElementoffsetXY(ItemId, offsetX, offset);

    }
    function holdingTopDivByVeriety() {
        window.clearTimeout(TopTimeByVeriety);
    }
    function hideTopDivByVeriety() {
        TopTimeByVeriety = setTimeout(TopDivHideByVeriety, 500);
    }
    function TopDivHideByVeriety() {
        if (TopDivByVeriety != null)
            TopDivByVeriety.Hide();
    }
</script>

